const articleModal = require('../../model')
const dayjs = require('dayjs')
const utc = require("dayjs/plugin/utc")
dayjs.extend(utc);
const DATA_TYPE_FORMAT = "YYYY-MM-DD HH:mm:ss";
// 获取列表
exports.gerArticle = (req, res) => {
  articleModal.ArticleSchema.find({}).lean().exec((err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 200,
      data: results
    })
  })
}

// 添加文章
exports.addArticle = (req, res) => {
  const article = req.body
  const time = dayjs.utc().utcOffset(8).format(DATA_TYPE_FORMAT)
  let addArticle = articleModal.ArticleSchema({
    ...article,
    likeNum: 0,
    collectNum: 0,
    commentNum: 0,
    creatTime: time
  })
  addArticle.save((err, result) => {
    if (err) return res.cc(err)
    res.send({ status: 200, message: "发送成功" })
  })
}

exports.getLikeNum = (req, res) => {
  articleModal.LikeSchema.find({ article_id: req.query.article_id }, (err, result) => {
    if (err) res.cc(err)
    res.send({
      status: 200,
      result
    })
  })
}